<?php

namespace Console\Logic;

use Common\Logic\BaseLogic;

class WithdrawalLogic extends BaseLogic
{
    public static $typeDic = array(
        1 => '提现中',
        2 => '提现成功',
        3 => '提现失败',
    );

    private $listModel;

    public function __construct()
    {
        parent::__construct();
        $this->listModel = D('Withdrawal');
    }

    /**
     * 返回类型字典.
     *
     * @return array
     */
    public function getTypeDic()
    {
        return static::$typeDic;
    }

    /**
     * 根据条件统计
     *
     * @param array $where
     *
     * @return mixed
     */
    public function statistics($where = array())
    {
        return $this->listModel->where($where)->count();
    }

    /**
     * 获取提现列表.
     *
     * @param int $page
     * @param int $size
     * @param $where
     *
     * @return mixed
     */
    public function getList($page = 1, $size = 20, $where)
    {
        $list = $this->listModel->where($where)->page($page, $size)->order('add_time desc')->select();
        $total = $this->statistics($where);

        return static::$res->suc('成功', array(
            'list' => $list,
            'total' => $total,
        ));
    }

    /**
     * 用户提现申请.
     *
     * @param [type] $auditor
     * @param [type] $id
     * @param int    $status
     * @param string $msg
     *
     * @return void
     */
    public function audit($auditor, $id, $status = 2, $msg = '')
    {
        if (empty($auditor)) {
            return static::$res->err('审核人不能为空');
        }
        if (empty($id)) {
            return static::$res->err('取现申请ID不能为空');
        }

        $where = array(
            'id' => $id,
            'state' => 1,
        );

        $info = $this->listModel->where($where)->find();
        if (empty($info)) {
            return static::$res->err('未找到取现申请');
        }

        /**
         * 获取提现用户信息.
         */
        $userLogic = A('Console/User', 'Logic');
        $userWhere = array(
            'user_state' => 1,
            'id' => $info['user_id'],
        );
        $userInfoRes = $userLogic->getInfo($userWhere);
        $userInfo = $userInfoRes['Result'];
        if (empty($userInfo)) {
            return static::$res->err('未找到用户信息');
        }

        $data = array(
            'state' => $status,
            'auditor' => $auditor,
            'update_time' => date('Y-m-d H:i:s'),
        );
        if (3 == $status) {
            if (empty($msg)) {
                $msg = '管理员驳回申请';
            }
            $data['error_msg'] = $msg;
        }

        $count = $this->listModel->where($where)->save($data);
        if (empty($count)) {
            return static::$res->err('数据库异常,操作失败');
        }

        if (2 != $status) {
            return static::$res->suc('操作成功');
        }

        $openId = $userInfo['openid'];
        $name = $info['name'];
        $sum = $info['sum'];

        $wxPayLogic = A('Game/WxPay', 'Logic');

        $transMsg = $wxPayLogic->trans($openId, $sum, $name);

        if (\is_bool($transMsg) && $transMsg) {
            return static::$res->suc('提现成功');
        }

        /**
         * 状态改为取现失败.
         */
        $data = array(
            'state' => 3,
            'error_msg' => $transMsg,
        );
        $this->listModel->where($where)->save($data);

        return static::$res->err($transMsg);
    }
}
